<template>
  <div class="app-container">
    <!--搜索拦-->
    <el-row :gutter="15" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          @click="handelAddTableDefinition"
          >新增
        </el-button>
      </el-col>
      <el-form
        :inline="true"
        :model="formData"
        class="el-form-search"
        @submit.prevent
      >
        <!--表名称输入-->
        <el-form-item
          class="el-form-search-item"
          label="表名称"
          porp="tableCnName"
        >
          <el-input v-model="formData.tableCnName" clearable></el-input>
        </el-form-item>

        <!-- 查询按钮 -->
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="loadData"
          >搜索
        </el-button>
      </el-form>
    </el-row>

    <!--表格-->
    <el-table border :data="tableData" :height="tableHeight" stripe>
      <el-table-column align="center" label="#" type="index" width="50">
        <template slot-scope="scope">
          <span>{{
            (formData.pageNum - 1) * formData.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表标识" prop="tablePrefix" align="center" />
      <el-table-column label="表名称" prop="tableCnName" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="修改时间" prop="updateTime" align="center" />
      <el-table-column label="创建者" prop="creator" align="center" />
      <el-table-column label="修改人" prop="modifier" align="center" />

      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="handelEditTableDefinition(scope.row)"
          >
            修改
          </el-button>
          <el-button
            size="small"
            type="text"
            @click="handelDelTableDefinition(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="800px"
    >
      <el-form
        ref="formDataRef"
        :model="submitData"
        :rules="rule"
        label-width="100px"
      >
        <!--第一行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="表标识" prop="tablePrefix">
              <el-input v-model="submitData.tablePrefix" placeholder="表标识" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文名称" prop="tableCnName">
              <el-input
                v-model="submitData.tableCnName"
                placeholder="中文名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormData">提交</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTableDefinition,
  addTableDefinition,
  editTableDefinition,
  deleteTableDefinition,
  getTableDefinitionById,
  deleteSettingQueryFields,
} from "@/api/system/settingQUeryFields";

export default {
  data() {
    return {
      formData: {
        pageNum: 1,
        pageSize: 30,
        tableCnName: "",
      },
      tableData: [],
      tableHeight: "calc(100vh - 200px)",

      submitData: {},
      title: "",
      open: false,

      rule: {
        tablePrefix: [
          {
            required: true,
            message: "表标识不能为空",
            trigger: "blur",
          },
        ],
        tableCnName: [
          {
            required: true,
            message: "中文名称不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    loadData() {
      getTableDefinition(this.formData).then((response) => {
        console.log(response);
        this.tableData = response.rows;
      });
    },
    handelAddTableDefinition(row) {
      //修改字段方法
      this.title = "新增表标识";
      this.open = true;
    },
    handelEditTableDefinition(row) {
      //修改字段方法
      // TODO 根据id查询表标识
      getTableDefinitionById(row.id).then((response) => {
        console.log(response);
        this.submitData = response.data;
      });
      console.log(row);
      this.title = "编辑表标识";
      this.open = true;
    },
    handelDelTableDefinition(row) {
      this.$modal
        .confirm("确认删除吗？")
        .then(function () {
          return deleteTableDefinition(row.id);
        })
        .then(() => {
          this.loadData();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    submitFormData() {
      console.log(this.submitData);
      this.$refs["formDataRef"].validate((valid) => {
        if (valid) {
          if (this.submitData.id !== undefined) {
            //修改
            editTableDefinition(this.submitData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.resetForm("formDataRef");
              this.loadData();
            });
          } else {
            //新增
            addTableDefinition(this.submitData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.resetForm("formDataRef");
              this.loadData();
            });
          }
        }
      });
    },
    close() {
      this.resetForm("formDataRef");
      this.open = false;
    },
  },
  mounted() {
    this.loadData();
  },
  created() {},
};
</script>

<style lang="scss" scoped></style>
